package com.frogs.ucenter.controller;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.util.UserMessageStatusEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.UserMessageDmService;

@Controller
public class UserMessageDmController extends AbsController {
	private static Logger log = LoggerFactory.getLogger(UserMessageDmController.class);    
	private static final String HOME_VIEW = "messageDm/index";
	private static final String HOME = "/messageDm/index";
	private static final String LIST = "/messageDm/list";
	private static final String READ = "/messageDm/read";
	private static final String UPDATE = "/messageDm/update";
	private static final String COUNT = "/messageDm/count";
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	@Resource
	private UserMessageDmService userMessageDmService;
	
    @RequestMapping(HOME)
    public String listUserMessageDm(HttpServletRequest request, ModelMap model) {
        return HOME_VIEW;
    }
	/**获取消息列表
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(LIST)
	public JSONObject getUserDmList(HttpServletRequest request,ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			int applicableId = requestInfo.getIntParameter("applicableId", ApplicablePlatformEnum.WEB.getId());//平台ID
			int status = requestInfo.getIntParameter("status", -1);//消息状态
			int pageNo = requestInfo.getIntParameter("pageNo", 0);
			int pageSize = requestInfo.getIntParameter("pageSize", 20);
			
			return userMessageDmService.getUserDmList(user.getId(), applicableId, status, user.getUserType(), pageNo, pageSize);
		} catch (Exception e) {
			log.error("func[getUserDmList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	/**单条读取
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(READ)
	public JSONObject userDmRead(HttpServletRequest request,ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			int applicableId = requestInfo.getIntParameter("applicableId", ApplicablePlatformEnum.WEB.getId());
			String ids = requestInfo.getParameter("ids");
			String ip = requestInfo.getParameter("ip", "");
			
			return userMessageDmService.userDmRead(user.getId(), applicableId, ids, ip, user.getUserType());
		} catch (Exception e) {
			log.error("func[getUserDmList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	/**删除，批量已读
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(UPDATE)
	public JSONObject userDmUpdate(HttpServletRequest request,ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			int applicableId = requestInfo.getIntParameter("applicableId", ApplicablePlatformEnum.WEB.getId());
			int status = requestInfo.getIntParameter("status", 0);
			String ids = requestInfo.getParameter("ids");
			String ip = requestInfo.getParameter("ip", "");
			
			return userMessageDmService.userDmUpdate(user.getId(), applicableId, ids, ip, status);
		} catch (Exception e) {
			log.error("func[getUserDmList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	/**获取未读消息数量
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(COUNT)
	public JSONObject userDmCount(HttpServletRequest request,ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			int applicableId = requestInfo.getIntParameter("applicableId", ApplicablePlatformEnum.WEB.getId());
			int status = requestInfo.getIntParameter("status", UserMessageStatusEnum.UNREAD.getId());
			
			return userMessageDmService.userDmCount(user.getId(), applicableId, user.getUserType(), status);
		} catch (Exception e) {
			log.error("func[getUserDmList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
}
